%% load data
clear; clc;

filename='C:\...\CMPR_Threshold\Data_141022.xlsx';
range_for_data='A2:J1841';      % variables start in third row (dependent variable, followed by 2 regressors)
years=46;       % this tells us how to slice stacked data
ccall=[1:40]';

data  =   xlsread(filename, 'data', range_for_data);

% construct dependent variable
Y=reshape(data(:,9),years,[]);      % dependent variable is Y
Y=Y';
[N,Tmax]=size(Y);                   % size N x Tmax
dY=reshape(data(:,5),years,[]); dY=dY';

% ae dummy
ae=reshape(data(:,4),years,[])';


%construct regressors
X1=reshape(data(:,10),years,[]); X1=X1';    % first regressor p
dX1=reshape(data(:,6),years,[]); dX1=dX1';    % first regressor dp

X2=reshape(data(:,8),years,[]); X2=X2';    % second regressor gd
dX2=reshape(data(:,7),years,[]); dX2=dX2';    % second regressor dgd

Z=[];                               % empty matrix Z (no regressors in Z)

% country codes
cc=reshape(data(:,2),years,[])';

%% sample selection
Y=Y; % level output
X1=X1; % price level - we difference data later 
X2=X2; % level debt

Yall=Y;
X1all=X1;
X2all=X2;
%% check if some time periods need to be trimmed (due to insufficient observations to compute cs averages)

Ay=not(isnan(Y));
ny=sum(Ay);
Ax1=not(isnan(X1));
nx1=sum(Ax1);
Ax2=not(isnan(X2));
nx2=sum(Ax2);

minumum_N=10; 
% find t_min
i=1;
while ny(1,i)<minumum_N || nx1(1,i)< minumum_N || nx2(1,i)< minumum_N
   i=i+1;
   if i>Tmax
      'error: none of the time periods have at least the chosen mimimum number of cross section units with available data'
      dbstop
   end
end
t_min=i;

i=Tmax;
while ny(1,i)<minumum_N || nx1(1,i)< minumum_N || nx2(1,i)< minumum_N
   i=i-1;
end
t_max=i;

% delete time periods with insufficient data
Y=Y(:,t_min:t_max);
X(:,:,1)=X2(:,t_min:t_max);  % debt goes as the first regressor (thresholds are based on the first variable)
% X(:,:,2)=X1(:,t_min:t_max);   % inflation (if desired), X can by (N,T,kx) with any kx (including 1). It is assumed that the first regressor is debt
tau_range=[1:11]'/10;

% advanced and emerging
seladv=ae(:,1)==1;
Yadv=Y(seladv,:);
Xadv=X(seladv,:,:);
tau_range_adv=tau_range;

seleme=ae(:,1)==0;
Yeme=Y(seleme,:);
Xeme=X(seleme,:,:);
tau_range_eme=tau_range;
% create differences
dX1=NaN(size(X(:,:,:))); dXadv1=NaN(size(Xadv(:,:,:))); dXeme1=NaN(size(Xeme(:,:,:)));

dX1(:,2:end,:)=X(:,2:end,:)-X(:,1:end-1,:);
dXadv1(:,2:end,:)=Xadv(:,2:end,:)-Xadv(:,1:end-1,:);
dXeme1(:,2:end,:)=Xeme(:,2:end,:)-Xeme(:,1:end-1,:);

%% estimation of threshold
S=10000; % number of simulations for the computation of critical values (ideally >10000)



%% Pooled and FE specifications

% Filtered pooled specification with d1x

%% robustness to lags

% FIRST ly=1, lx=1
% 
% Basic specification A_ly1lx1_: FE, Lag growth, contemporaneous dx1 
lx=1;%0; % -1 for no contemporaneous values and no lags of debt and inflation
ldy=1; % 0 for no lagged dependent variable
lz=0; % contemporaneous
dflz=1; % lagged values of z enter only in differenced form if set to 1, otherwise in levels
                % if lz=1 and dflz=1, then zit enter the set of pooled regressors and dzit enters in Xi
                % if lz=1 and dflz=0, then zit enter the set of pooled regressors and zit-1 enters in Xi
                % if lz=0, then only zit enter the set of pooled regressors (regardless of dflz)
lcsx=-1; %1; %-1; % no contemporaneous values and no lags of cs ave of debt and inflation
lcsdy=-1; %1; %-1; % no contemporaneous values and no lags of cs ave of dependent variable
lcsz=-1; %1; %-1; % % no contemporaneous values and no lags of cs ave of indicator variable

topool=false(4,1); % selects which variables to pool in addition to the indicator variable, which is always pooled. See create_regressors_for_threshold_estim for the ordering of individual variables
debt_for_ind=X(:,:,1); debt_for_ind_adv=Xadv(:,:,1); debt_for_ind_eme=Xeme(:,:,1);

[ RA_ly1lx1] = estim_all_adv_eme(lz, dflz,lx,ldy,lcsx,lcsdy, lcsz, topool,debt_for_ind, debt_for_ind_adv, debt_for_ind_eme,Y, Yadv, Yeme, dX1,dXadv1,dXeme1,@indf,@indit_f,@ind2f,tau_range,S,seladv, seleme);
xlswrite('C:\...\CMPR_Threshold\results_case1.xlsx',RA_ly1lx1,1,'BH6:DF64');


% SECOND, ly=2, lx=2;

% Basic specification A_ly2lx2_: FE, Lag growth, contemporaneous dx1 
lx=2;%0; % -1 for no contemporaneous values and no lags of debt and inflation
ldy=2; % 0 for no lagged dependent variable
lz=0; % contemporaneous
dflz=1; % lagged values of z enter only in differenced form if set to 1, otherwise in levels
                % if lz=1 and dflz=1, then zit enter the set of pooled regressors and dzit enters in Xi
                % if lz=1 and dflz=0, then zit enter the set of pooled regressors and zit-1 enters in Xi
                % if lz=0, then only zit enter the set of pooled regressors (regardless of dflz)
lcsx=-1; %1; %-1; % no contemporaneous values and no lags of cs ave of debt and inflation
lcsdy=-1; %1; %-1; % no contemporaneous values and no lags of cs ave of dependent variable
lcsz=-1; %1; %-1; % % no contemporaneous values and no lags of cs ave of indicator variable

topool=false(6,1); % selects which variables to pool in addition to the indicator variable, which is always pooled. See create_regressors_for_threshold_estim for the ordering of individual variables
debt_for_ind=X(:,:,1); debt_for_ind_adv=Xadv(:,:,1); debt_for_ind_eme=Xeme(:,:,1);

[ RA_ly2lx2] = estim_all_adv_eme(lz, dflz,lx,ldy,lcsx,lcsdy, lcsz, topool,debt_for_ind, debt_for_ind_adv, debt_for_ind_eme,Y, Yadv, Yeme, dX1,dXadv1,dXeme1,@indf,@indit_f,@ind2f,tau_range,S,seladv, seleme);
xlswrite('C:\...\CMPR_Threshold\results_case1.xlsx',RA_ly2lx2,2,'BH6:DF64');

% THIRD, ly=3, lx=3;

% Basic specification A_ly2lx2_: FE, Lag growth, contemporaneous dx1 
lx=3;%0; % -1 for no contemporaneous values and no lags of debt and inflation
ldy=3; % 0 for no lagged dependent variable
lz=0; % contemporaneous
dflz=1; % lagged values of z enter only in differenced form if set to 1, otherwise in levels
                % if lz=1 and dflz=1, then zit enter the set of pooled regressors and dzit enters in Xi
                % if lz=1 and dflz=0, then zit enter the set of pooled regressors and zit-1 enters in Xi
                % if lz=0, then only zit enter the set of pooled regressors (regardless of dflz)
lcsx=-1; %1; %-1; % no contemporaneous values and no lags of cs ave of debt and inflation
lcsdy=-1; %1; %-1; % no contemporaneous values and no lags of cs ave of dependent variable
lcsz=-1; %1; %-1; % % no contemporaneous values and no lags of cs ave of indicator variable

topool=false(8,1); % selects which variables to pool in addition to the indicator variable, which is always pooled. See create_regressors_for_threshold_estim for the ordering of individual variables
debt_for_ind=X(:,:,1); debt_for_ind_adv=Xadv(:,:,1); debt_for_ind_eme=Xeme(:,:,1);

[ RA_ly3lx3] = estim_all_adv_eme(lz, dflz,lx,ldy,lcsx,lcsdy, lcsz, topool,debt_for_ind, debt_for_ind_adv, debt_for_ind_eme,Y, Yadv, Yeme, dX1,dXadv1,dXeme1,@indf,@indit_f,@ind2f,tau_range,S,seladv, seleme);
xlswrite('C:\...\CMPR_Threshold\results_case1.xlsx',RA_ly3lx3,3,'BH6:DF64');

%% robustness to CS averages

% same as with lags but add cs ave

% FIRST ly=1, lx=1

% Basic specification Alcs_ly1lx1_: FE, Lag growth, contemporaneous dx1 
lx=1;%0; % -1 for no contemporaneous values and no lags of debt and inflation
ldy=1; % 0 for no lagged dependent variable
lz=0; % contemporaneous
dflz=1; % lagged values of z enter only in differenced form if set to 1, otherwise in levels
                % if lz=1 and dflz=1, then zit enter the set of pooled regressors and dzit enters in Xi
                % if lz=1 and dflz=0, then zit enter the set of pooled regressors and zit-1 enters in Xi
                % if lz=0, then only zit enter the set of pooled regressors (regardless of dflz)
lcsx=1; %1; %-1; % no contemporaneous values and no lags of cs ave of debt and inflation
lcsdy=1; %1; %-1; % no contemporaneous values and no lags of cs ave of dependent variable
lcsz=0; %1; %-1; % % no contemporaneous values and no lags of cs ave of indicator variable

topool=false(8,1); % selects which variables to pool in addition to the indicator variable, which is always pooled. See create_regressors_for_threshold_estim for the ordering of individual variables
debt_for_ind=X(:,:,1); debt_for_ind_adv=Xadv(:,:,1); debt_for_ind_eme=Xeme(:,:,1);
[ RAlcs_ly1lx1 ] = estim_all_adv_eme(lz, dflz,lx,ldy,lcsx,lcsdy, lcsz, topool,debt_for_ind, debt_for_ind_adv, debt_for_ind_eme,Y, Yadv, Yeme, dX1,dXadv1,dXeme1,@indf,@indit_f,@ind2f,tau_range,S,seladv, seleme);
xlswrite('C:\...\CMPR_Threshold\results_case1.xlsx',RAlcs_ly1lx1,4,'BH6:DF64');

% SECOND, ly=2, lx=2;
% Basic specification Alcs_ly2lx2_: FE, Lag growth, contemporaneous dx1 
lx=2;%0; % -1 for no contemporaneous values and no lags of debt and inflation
ldy=2; % 0 for no lagged dependent variable
lz=0; % contemporaneous
dflz=1; % lagged values of z enter only in differenced form if set to 1, otherwise in levels
                % if lz=1 and dflz=1, then zit enter the set of pooled regressors and dzit enters in Xi
                % if lz=1 and dflz=0, then zit enter the set of pooled regressors and zit-1 enters in Xi
                % if lz=0, then only zit enter the set of pooled regressors (regardless of dflz)
lcsx=2; %1; %-1; % no contemporaneous values and no lags of cs ave of debt and inflation
lcsdy=2; %1; %-1; % no contemporaneous values and no lags of cs ave of dependent variable
lcsz=0; %1; %-1; % % no contemporaneous values and no lags of cs ave of indicator variable

topool=false(12,1); % selects which variables to pool in addition to the indicator variable, which is always pooled. See create_regressors_for_threshold_estim for the ordering of individual variables
debt_for_ind=X(:,:,1); debt_for_ind_adv=Xadv(:,:,1); debt_for_ind_eme=Xeme(:,:,1);
[ RAlcs_ly2lx2] = estim_all_adv_eme(lz, dflz,lx,ldy,lcsx,lcsdy, lcsz, topool,debt_for_ind, debt_for_ind_adv, debt_for_ind_eme,Y, Yadv, Yeme, dX1,dXadv1,dXeme1,@indf,@indit_f,@ind2f,tau_range,S,seladv, seleme);
xlswrite('C:\...\CMPR_Threshold\results_case1.xlsx',RAlcs_ly2lx2,5,'BH6:DF64');


% THIRD, ly=3, lx=3;
% Basic specification Alcs_ly3lx3_: FE, Lag growth, contemporaneous dx1 
lx=3;%0; % -1 for no contemporaneous values and no lags of debt and inflation
ldy=3; % 0 for no lagged dependent variable
lz=0; % contemporaneous
dflz=1; % lagged values of z enter only in differenced form if set to 1, otherwise in levels
                % if lz=1 and dflz=1, then zit enter the set of pooled regressors and dzit enters in Xi
                % if lz=1 and dflz=0, then zit enter the set of pooled regressors and zit-1 enters in Xi
                % if lz=0, then only zit enter the set of pooled regressors (regardless of dflz)
lcsx=3; %1; %-1; % no contemporaneous values and no lags of cs ave of debt and inflation
lcsdy=3; %1; %-1; % no contemporaneous values and no lags of cs ave of dependent variable
lcsz=0; %1; %-1; % % no contemporaneous values and no lags of cs ave of indicator variable

topool=false(16,1); % selects which variables to pool in addition to the indicator variable, which is always pooled. See create_regressors_for_threshold_estim for the ordering of individual variables
debt_for_ind=X(:,:,1); debt_for_ind_adv=Xadv(:,:,1); debt_for_ind_eme=Xeme(:,:,1);
[ RAlcs_ly3lx3] = estim_all_adv_eme(lz, dflz,lx,ldy,lcsx,lcsdy, lcsz, topool,debt_for_ind, debt_for_ind_adv, debt_for_ind_eme,Y, Yadv, Yeme, dX1,dXadv1,dXeme1,@indf,@indit_f,@ind2f,tau_range,S,seladv, seleme);
xlswrite('C:\...\CMPR_Threshold\results_case1.xlsx',RAlcs_ly3lx3,6,'BH6:DF64');

%% now DL with lagged indicator as well (differences)

%p=0
    %no cs
lx=0;%0; % -1 for no contemporaneous values and no lags of debt and inflation
ldy=0; % 0 for no lagged dependent variable
lz=lx+1; % contemporaneous
dflz=1; % lagged values of z enter only in differenced form if set to 1, otherwise in levels
                %if lz=1 and dflz=1, then zit enter the set of pooled regressors and dzit enters in Xi
                %if lz=1 and dflz=0, then zit enter the set of pooled regressors and zit-1 enters in Xi
                %if lz=0, then only zit enter the set of pooled regressors (regardless of dflz)
lcsx=-1; %1; %-1; % no contemporaneous values and no lags of cs ave of debt and inflation
lcsdy=-1; %1; %-1; % no contemporaneous values and no lags of cs ave of dependent variable
lcsz=-1; %1; %-1; % % no contemporaneous values and no lags of cs ave of indicator variable


topool=false(2,1); % selects which variables to pool in addition to the indicator variable, which is always pooled. See create_regressors_for_threshold_estim for the ordering of individual variables
debt_for_ind=X(:,:,1); debt_for_ind_adv=Xadv(:,:,1); debt_for_ind_eme=Xeme(:,:,1);

[ RD_0] = estim_all_adv_eme(lz, dflz,lx,ldy,lcsx,lcsdy, lcsz, topool,debt_for_ind, debt_for_ind_adv, debt_for_ind_eme,Y, Yadv, Yeme, dX1,dXadv1,dXeme1,@indf,@indit_f,@ind2f,tau_range,S,seladv, seleme);
xlswrite('C:\...\CMPR_Threshold\results_case1.xlsx',RD_0,7,'BH6:DF64');

%p=1
    %no cs
lx=1;%0; % -1 for no contemporaneous values and no lags of debt and inflation
ldy=0; % 0 for no lagged dependent variable
lz=lx+1; % contemporaneous
dflz=1; % lagged values of z enter only in differenced form if set to 1, otherwise in levels
                % if lz=1 and dflz=1, then zit enter the set of pooled regressors and dzit enters in Xi
                % if lz=1 and dflz=0, then zit enter the set of pooled regressors and zit-1 enters in Xi
                % if lz=0, then only zit enter the set of pooled regressors (regardless of dflz)
lcsx=-1; %1; %-1; % no contemporaneous values and no lags of cs ave of debt and inflation
lcsdy=-1; %1; %-1; % no contemporaneous values and no lags of cs ave of dependent variable
lcsz=-1; %1; %-1; % % no contemporaneous values and no lags of cs ave of indicator variable


topool=false(3,1); % selects which variables to pool in addition to the indicator variable, which is always pooled. See create_regressors_for_threshold_estim for the ordering of individual variables
debt_for_ind=X(:,:,1); debt_for_ind_adv=Xadv(:,:,1); debt_for_ind_eme=Xeme(:,:,1);

[ RD_1] = estim_all_adv_eme(lz, dflz,lx,ldy,lcsx,lcsdy, lcsz, topool,debt_for_ind, debt_for_ind_adv, debt_for_ind_eme,Y, Yadv, Yeme, dX1,dXadv1,dXeme1,@indf,@indit_f,@ind2f,tau_range,S,seladv, seleme);
xlswrite('C:\...\CMPR_Threshold\results_case1.xlsx',RD_1,8,'BH6:DF64');

%p=2
    %no cs
lx=2;%0; % -1 for no contemporaneous values and no lags of debt and inflation
ldy=0; % 0 for no lagged dependent variable
lz=lx+1; % contemporaneous
dflz=1; % lagged values of z enter only in differenced form if set to 1, otherwise in levels
                % if lz=1 and dflz=1, then zit enter the set of pooled regressors and dzit enters in Xi
                % if lz=1 and dflz=0, then zit enter the set of pooled regressors and zit-1 enters in Xi
                % if lz=0, then only zit enter the set of pooled regressors (regardless of dflz)
lcsx=-1; %1; %-1; % no contemporaneous values and no lags of cs ave of debt and inflation
lcsdy=-1; %1; %-1; % no contemporaneous values and no lags of cs ave of dependent variable
lcsz=-1; %1; %-1; % % no contemporaneous values and no lags of cs ave of indicator variable


topool=false(4,1); % selects which variables to pool in addition to the indicator variable, which is always pooled. See create_regressors_for_threshold_estim for the ordering of individual variables
debt_for_ind=X(:,:,1); debt_for_ind_adv=Xadv(:,:,1); debt_for_ind_eme=Xeme(:,:,1);

[ RD_2] = estim_all_adv_eme(lz, dflz,lx,ldy,lcsx,lcsdy, lcsz, topool,debt_for_ind, debt_for_ind_adv, debt_for_ind_eme,Y, Yadv, Yeme, dX1,dXadv1,dXeme1,@indf,@indit_f,@ind2f,tau_range,S,seladv, seleme);
xlswrite('C:\...\CMPR_Threshold\results_case1.xlsx',RD_2,9,'BH6:DF64');   

%p=3
    %no cs
lx=3;%0; % -1 for no contemporaneous values and no lags of debt and inflation
ldy=0; % 0 for no lagged dependent variable
lz=lx+1; % contemporaneous
dflz=1; % lagged values of z enter only in differenced form if set to 1, otherwise in levels
                % if lz=1 and dflz=1, then zit enter the set of pooled regressors and dzit enters in Xi
                % if lz=1 and dflz=0, then zit enter the set of pooled regressors and zit-1 enters in Xi
                % if lz=0, then only zit enter the set of pooled regressors (regardless of dflz)
lcsx=-1; %1; %-1; % no contemporaneous values and no lags of cs ave of debt and inflation
lcsdy=-1; %1; %-1; % no contemporaneous values and no lags of cs ave of dependent variable
lcsz=-1; %1; %-1; % % no contemporaneous values and no lags of cs ave of indicator variable


topool=false(5,1); % selects which variables to pool in addition to the indicator variable, which is always pooled. See create_regressors_for_threshold_estim for the ordering of individual variables
debt_for_ind=X(:,:,1); debt_for_ind_adv=Xadv(:,:,1); debt_for_ind_eme=Xeme(:,:,1);

[ RD_3] = estim_all_adv_eme(lz, dflz,lx,ldy,lcsx,lcsdy, lcsz, topool,debt_for_ind, debt_for_ind_adv, debt_for_ind_eme,Y, Yadv, Yeme, dX1,dXadv1,dXeme1,@indf,@indit_f,@ind2f,tau_range,S,seladv, seleme);
xlswrite('C:\...\CMPR_Threshold\results_case1.xlsx',RD_3,10,'BH6:DF64');

% with cs

tau_range=[1:11]'/10;
%p=0
lx=0;%0; % -1 for no contemporaneous values and no lags of debt and inflation
ldy=0; % 0 for no lagged dependent variable
lz=0; % contemporaneous
dflz=1; % lagged values of z enter only in differenced form if set to 1, otherwise in levels
                % if lz=1 and dflz=1, then zit enter the set of pooled regressors and dzit enters in Xi
                % if lz=1 and dflz=0, then zit enter the set of pooled regressors and zit-1 enters in Xi
                % if lz=0, then only zit enter the set of pooled regressors (regardless of dflz)
lcsx=lx; %1; %-1; % no contemporaneous values and no lags of cs ave of debt and inflation
lcsdy=0; %1; %-1; % no contemporaneous values and no lags of cs ave of dependent variable
lcsz=lz; %1; %-1; % % no contemporaneous values and no lags of cs ave of indicator variable


topool=false(4,1); % selects which variables to pool in addition to the indicator variable, which is always pooled. See create_regressors_for_threshold_estim for the ordering of individual variables
debt_for_ind=X(:,:,1); debt_for_ind_adv=Xadv(:,:,1); debt_for_ind_eme=Xeme(:,:,1);

[ RD_0cs] = estim_all_adv_eme(lz, dflz,lx,ldy,lcsx,lcsdy, lcsz, topool,debt_for_ind, debt_for_ind_adv, debt_for_ind_eme,Y, Yadv, Yeme, dX1,dXadv1,dXeme1,@indf,@indit_f,@ind2f,tau_range,S,seladv, seleme);
xlswrite('C:\...\CMPR_Threshold\results_case1.xlsx',RD_0cs,11,'BH6:DF64');

%p=1
lx=1;%0; % -1 for no contemporaneous values and no lags of debt and inflation
ldy=0; % 0 for no lagged dependent variable
lz=0; % contemporaneous
dflz=1; % lagged values of z enter only in differenced form if set to 1, otherwise in levels
                % if lz=1 and dflz=1, then zit enter the set of pooled regressors and dzit enters in Xi
                % if lz=1 and dflz=0, then zit enter the set of pooled regressors and zit-1 enters in Xi
                % if lz=0, then only zit enter the set of pooled regressors (regardless of dflz)
lcsx=lx; %1; %-1; % no contemporaneous values and no lags of cs ave of debt and inflation
lcsdy=0; %1; %-1; % no contemporaneous values and no lags of cs ave of dependent variable
lcsz=lz; %1; %-1; % % no contemporaneous values and no lags of cs ave of indicator variable


topool=false(6,1); % selects which variables to pool in addition to the indicator variable, which is always pooled. See create_regressors_for_threshold_estim for the ordering of individual variables
debt_for_ind=X(:,:,1); debt_for_ind_adv=Xadv(:,:,1); debt_for_ind_eme=Xeme(:,:,1);

[ RD_1cs] = estim_all_adv_eme(lz, dflz,lx,ldy,lcsx,lcsdy, lcsz, topool,debt_for_ind, debt_for_ind_adv, debt_for_ind_eme,Y, Yadv, Yeme, dX1,dXadv1,dXeme1,@indf,@indit_f,@ind2f,tau_range,S,seladv, seleme);
xlswrite('C:\...\CMPR_Threshold\results_case1.xlsx',RD_1cs,12,'BH6:DF64');

%p=2
lx=2;%0; % -1 for no contemporaneous values and no lags of debt and inflation
ldy=0; % 0 for no lagged dependent variable
lz=0; % contemporaneous
dflz=1; % lagged values of z enter only in differenced form if set to 1, otherwise in levels
                % if lz=1 and dflz=1, then zit enter the set of pooled regressors and dzit enters in Xi
                % if lz=1 and dflz=0, then zit enter the set of pooled regressors and zit-1 enters in Xi
                % if lz=0, then only zit enter the set of pooled regressors (regardless of dflz)
lcsx=lx; %1; %-1; % no contemporaneous values and no lags of cs ave of debt and inflation
lcsdy=0; %1; %-1; % no contemporaneous values and no lags of cs ave of dependent variable
lcsz=lz; %1; %-1; % % no contemporaneous values and no lags of cs ave of indicator variable


topool=false(8,1); % selects which variables to pool in addition to the indicator variable, which is always pooled. See create_regressors_for_threshold_estim for the ordering of individual variables
debt_for_ind=X(:,:,1); debt_for_ind_adv=Xadv(:,:,1); debt_for_ind_eme=Xeme(:,:,1);

[ RD_2cs] = estim_all_adv_eme(lz, dflz,lx,ldy,lcsx,lcsdy, lcsz, topool,debt_for_ind, debt_for_ind_adv, debt_for_ind_eme,Y, Yadv, Yeme, dX1,dXadv1,dXeme1,@indf,@indit_f,@ind2f,tau_range,S,seladv, seleme);
xlswrite('C:\...\CMPR_Threshold\results_case1.xlsx',RD_2cs,13,'BH6:DF64');   

%p=3 
lx=3;%0; % -1 for no contemporaneous values and no lags of debt and inflation
ldy=0; % 0 for no lagged dependent variable
lz=0; % contemporaneous
dflz=1; % lagged values of z enter only in differenced form if set to 1, otherwise in levels
                % if lz=1 and dflz=1, then zit enter the set of pooled regressors and dzit enters in Xi
                % if lz=1 and dflz=0, then zit enter the set of pooled regressors and zit-1 enters in Xi
                % if lz=0, then only zit enter the set of pooled regressors (regardless of dflz)
lcsx=lx; %1; %-1; % no contemporaneous values and no lags of cs ave of debt and inflation
lcsdy=0; %1; %-1; % no contemporaneous values and no lags of cs ave of dependent variable
lcsz=lz; %1; %-1; % % no contemporaneous values and no lags of cs ave of indicator variable


topool=false(10,1); % selects which variables to pool in addition to the indicator variable, which is always pooled. See create_regressors_for_threshold_estim for the ordering of individual variables
debt_for_ind=X(:,:,1); debt_for_ind_adv=Xadv(:,:,1); debt_for_ind_eme=Xeme(:,:,1);

[ RD_3cs] = estim_all_adv_eme(lz, dflz,lx,ldy,lcsx,lcsdy, lcsz, topool,debt_for_ind, debt_for_ind_adv, debt_for_ind_eme,Y, Yadv, Yeme, dX1,dXadv1,dXeme1,@indf,@indit_f,@ind2f,tau_range,S,seladv, seleme);
xlswrite('C:\...\CMPR_Threshold\results_case1.xlsx',RD_3cs,14,'BH6:DF64');   

'end'